package com.example.itshome.network;

import android.app.Activity;
import com.example.itshome.entity.ActivityEntity;
import com.example.itshome.manager.UserInfoManager;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class GetFinishActivityRequest extends AbstractRecycleVIewGetRequest<ActivityEntity>{
    public GetFinishActivityRequest(Activity activity) {
        super(activity);
    }

    @Override
    protected String getPathAndParams() {
        return "activity/joined-end?username="+ UserInfoManager.getInstance().getUser().getUsername();
    }

    @Override
    protected void handleResult(String result) {
        try {
            JSONObject jsonObject=new JSONObject(result);
            int code=jsonObject.getInt("code");
            if (code!=200){
                return;
            }
            List<ActivityEntity> list=new ArrayList<>();
            JSONArray data= jsonObject.getJSONArray("data");
            int len=data.length();
            for(int i=0;i<len;i++){
                JSONObject item=data.getJSONObject(i);
                ActivityEntity entity=new ActivityEntity()
                        .setTitle(item.getString("name"))
                        .setDescription(item.getString("activInfo"))
                        .setId(item.getInt("activId"))
                        .setLocate(item.getString("activLocation"))
                        .setTime(item.getString("startTime"))
                        .setOrganizationName(item.getString("organizationName"));
                list.add(entity);
            }
            setHandleResultRunnable(new Runnable() {
                @Override
                public void run() {
                    for (ActivityEntity entity:list){
                        addItem(entity);
                    }
                }
            });
        }catch (JSONException e){
            e.printStackTrace();
        }
    }
}
